package com.example.administrator.myapplication.View.Fragment;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.administrator.myapplication.Bean.Label;
import com.example.administrator.myapplication.Bean.Picture;
import com.example.administrator.myapplication.Http.Api;
import com.example.administrator.myapplication.Presenter.LabelPresenter;
import com.example.administrator.myapplication.Presenter.PicturePersenter;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.Util.SharedPreferencesUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2019/10/13 0013.
 */

public class TagFragment extends Fragment implements IPictureView,ILabelView,View.OnClickListener{
    private ImageView iv_image;
    private TextView tv_label;
    private RadioButton rb_tag;
    private RadioButton rb_fresh;
    private RadioButton rb_skip;
    private RadioButton rb_submit;
    private Dialog dialog;
    private String remark;
    private String label;
    private Picture picture;
    private PicturePersenter picturePersenter;
    private LabelPresenter labelPresenter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tag,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }
    private void init(View view){
        iv_image = view.findViewById(R.id.iv_image);
        tv_label = view.findViewById(R.id.tv_label);
        rb_tag = view.findViewById(R.id.rb_tag);
        rb_fresh = view.findViewById(R.id.rb_fresh);
        rb_skip = view.findViewById(R.id.rb_skip);
        rb_submit = view.findViewById(R.id.rb_submit);
        rb_tag.setOnClickListener(this);
        rb_fresh.setOnClickListener(this);
        rb_skip.setOnClickListener(this);
        rb_submit.setOnClickListener(this);
        picturePersenter = new PicturePersenter(this);
        labelPresenter = new LabelPresenter(this);


        picturePersenter.getRandomPicture();
    }

    private Dialog createDialog(){
        if(dialog!=null){
            dialog.dismiss();
            dialog=null;
        }
            final EditText ed_temp = new EditText(getActivity());
            ed_temp.setText(picture.getRemark());
            dialog = new AlertDialog.Builder(getActivity())
                .setTitle("请输入标签")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setView(ed_temp)
                .setCancelable(true)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        label = ed_temp.getText().toString();
                        tv_label.setText(label);
                    }
                })
                .create();
        return dialog;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rb_tag :
                /**
                 * 创建对话框
                 * */
                createDialog();
                dialog.show();
                break;
            case R.id.rb_fresh:
                showPicture(picture);
                createDialog();
                break;
            case R.id.rb_skip:
                picturePersenter.getRandomPicture();
                createDialog();
                break;
            case R.id.rb_submit:
                if (label!=null&&!"".equals(label)&&picture!=null){
                    Label a = new Label(SharedPreferencesUtil.getUser(getActivity()).getUserId(),picture.getPictureId(),label,new Date());
                    labelPresenter.addLabel(a);
                }
                break;
        }
    }


    @Override
    public void showPicture(Picture picture) {
        label = "";
        tv_label.setText(label);
        if(picture!=null){
            this.picture = picture;
            RequestOptions requestOptions = new RequestOptions()
                    .error(R.drawable.captcha_loadfail)
                    .placeholder(R.drawable.captcha_loadfail)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(true);
            Glide.with(this)
                    .asBitmap()
                    .load(Api.Server_Host+"/"+picture.getSrcPath())
                    .apply(requestOptions)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            int width = resource.getWidth();
                            int height = resource.getHeight();
                            //获取imageView的宽
                            int imageViewWidth = iv_image.getWidth();
                            //计算缩放比例
                            float sy = (float)imageViewWidth/(float)width;
                            //计算图片等比例放大后的高
                            int imageViewHeight = (int)(height * sy);
                            ViewGroup.LayoutParams params = iv_image.getLayoutParams();
                            params.height = imageViewHeight;
                            iv_image.setLayoutParams(params);
                            iv_image.setImageBitmap(resource);
                        }
                    });
        }
    }

    @Override
    //标签提交成功后
    public void showSuccess(String msg) {
        Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
        picturePersenter.getRandomPicture();
        createDialog();
    }

    @Override
    public void showError(String msg) {
        Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLables(List<HashMap> labels) {

    }
}
